package net.libra.utils
{
	import flash.utils.Dictionary;
	public class ObjectPool 
	{
		private static var _pool:Dictionary = new Dictionary(true);
		
		
		private var _class:Class;
		private var _list:Array;
		
		public function ObjectPool(value:Class)
		{
			_class = value;
			_pool[_class] = this;
			clear();
		}
		public function getClass():Class
		{
			return _class;
		}
		public function clear():void
		{
			_list = [];
		}
		public function get length():uint
		{
			return _list.length;
		}
		public function borrowObject(...arg):Object
		{
			if(_list.length > 0)
			{
				return _list.shift();
			}
			var obj:Object;
			switch(arg.length)
			{
				case 1:obj = new _class(arg[0]);break;
				case 2:obj = new _class(arg[0],arg[1]);break;
				case 3:obj = new _class(arg[0],arg[1],arg[2]);break;
				case 4:obj = new _class(arg[0],arg[1],arg[2],arg[3]);break;
				case 5:obj = new _class(arg[0],arg[1],arg[2],arg[3],arg[4]);break;
				case 6:obj = new _class(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5]);break;
				case 7:obj = new _class(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6]);break;
				case 8:obj = new _class(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],arg[7]);break;
				case 9:obj = new _class(arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],arg[7],arg[8]);break;
				default:obj = new _class();
			}
			return obj;
		}
		
		public function returnObject(value:Object):void
		{
			_list.push(value);
		}
		
		public function destroy():void
		{
			clear();
			delete _pool[getClass()];
			_class = null;
			_list = null;
		}
		
		public static function getObjectPool(value:Class):ObjectPool
		{
			if(!_pool[value])
			{
				_pool[value] = new ObjectPool(value);
			}
			return _pool[value];
		}
	}
}